<?php

namespace app\mainadmin\model;
use app\BaseModel;
use app\member\model\AccountModel;
use think\Db;
use utils\Time;

//*------------------------------------------------------ */
//-- 系统设置
/*------------------------------------------------------ */
class TaskingModel extends BaseModel
{
	protected $table = 'main_tasking';

	public $pk = 'id';

    public $type = ['1'=>'单次','2'=>'每天多次','3'=>'每天无限制'];

    public $status = ['0'=>'下架','1'=>'上架'];

    /*------------------------------------------------------ */
    //-- 获取菜单
    /*------------------------------------------------------ */
    public function getList()
    {
        $rows = self::order('sort DESC,id DESC')->select()->toArray();
        return $rows;
    }

    public function completeTask($user_id, $tasking_info, $change_type, $change_desc, $isTrans = true){
        if ($tasking_info['status'] == 1) {
            list($start, $end) = Time::today();
            $taskingLogModel = new TaskingLogModel();
            $count_tasking = $taskingLogModel
                ->where('tasking_id', $tasking_info['id'])
                ->where('user_id', $user_id)
                ->whereBetweenTime('createtime', $start, $end)
                ->count();
            if ($tasking_info['type'] == 1 && $count_tasking >= 1) return true;
            if ($tasking_info['type'] == 2 && $count_tasking >= $tasking_info['task_nums']) return true;

            if ($isTrans) Db::startTrans();

            if ($taskingLogModel->where("status!=1 and tasking_id={$tasking_info['id']} and user_id={$user_id}")->find()) {
                $where_s[] = ['status', '<>', 1];
                $where_s[] = ['tasking_id', '=', $tasking_info['id']];
                $where_s[] = ['user_id', '=', $user_id];
                if (!$taskingLogModel->where($where_s)->update(['status' => 1])) {
                    if ($isTrans) Db::rollback();
                    return false;
                }
            } else {
                if (!$taskingLogModel->insert(['user_id' => $user_id, 'tasking_id' => $tasking_info['id'], 'status' => 1, 'createtime' => time()])) {
                    if ($isTrans) Db::rollback();
                    return false;
                }
            }

            $userAccount = new AccountModel();
            $accountData['use_integral'] = $tasking_info['points'];
            $accountData['change_desc'] = $change_desc;
            $accountData['user_id'] = $user_id;
            $accountData['change_type'] = $change_type;
            $res = $userAccount->change($accountData, $user_id, false);

            if ($res){
                if ($isTrans) Db::commit();
                return true;
            }
            Db::commit();
            return false;
        }

    }

    public function inviteFriends($user_id){
        $tasking_info = $this->where('id', 2)->find();
        return $this->completeTask($user_id, $tasking_info, 22, '邀请好友');
    }

    public function shareGoods($user_id, $isTrans = true){
        $tasking_info = $this->where('id', 4)->find();
        return $this->completeTask($user_id, $tasking_info, 23, '分享商品', $isTrans);
    }


}
